package com.man;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.std.DB;
import com.std.Type;

public class TypeCtrl {
	private DB db;
	public TypeCtrl(){
		db=new DB();
	}
	public void insertItem(String typeName) {
		String sql="insert into type_t values(type_s.nextval,?)";
		Object []params={typeName};
		db.execute(sql, params);

	}
	public List<Type> getItems(){
		List<Type> list=new ArrayList<Type>();
		String sql="select type_t.*,(select count(*) from book_t where book_t.type_id=type_t.type_id) cnt from type_t";
		db.execute(sql, null);
		ResultSet rs=db.getResultset();
		try {
			while(rs.next()){
				Type type=new Type();
				type.setBookCount(rs.getInt("cnt"));
				type.setTypeId(rs.getInt("type_id"));
				type.setTypeName(rs.getString("type_name"));
				list.add(type);
			}		
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		
		return list;
	}
	public void deleteItem(int typeId) {
		String sql="delete from type_t ";
		Object []params=new Object[1];
		int length=0;
		if(typeId!=0){
			sql=sql+"where type_id=?";
			params[length++]=typeId;
		}
		db.execute(sql, params);
		
	}	
	public void close() {
		db.close();
	}
}
